package com.ht.pms.core.dto.poi;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ht.pms.core.common.constant.SalaryConstants;
import com.ht.pms.core.dto.PageRequest;
import com.ht.pms.core.dto.dict.DictParamsDto;

public class ExcelExportQuerySqlReq extends PageRequest{
	
	private String tableName;
	
	private Integer fileId;
	
	private Integer inState;
	
	/** 下载数据管理部分  */
	private String batchCode;
	
	private String paydayTimeYear;  //发薪时间
	
    private String templateCode;  //工资表名
    private String keyworld;  
    
    private String jobCodes; // 复选框数据
	
	private List<DictParamsDto> dictParams;

	public ExcelExportQuerySqlReq() {
		super();
	}

	public ExcelExportQuerySqlReq(String tableName, Integer fileId, Integer inState) {
		super();
		this.tableName = tableName;
		this.fileId = fileId;
		this.inState = inState;
	}
	
	

	public ExcelExportQuerySqlReq(String tableName, Integer fileId, Integer inState,
			List<DictParamsDto> dictParams) {
		super();
		this.tableName = tableName;
		this.fileId = fileId;
		this.inState = inState;
		this.dictParams = dictParams;
	}
	
	

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Integer getFileId() {
		return fileId;
	}

	public void setFileId(Integer fileId) {
		this.fileId = fileId;
	}

	public Integer getInState() {
		return inState;
	}

	public void setInState(Integer inState) {
		this.inState = inState;
	}
	
	public List<DictParamsDto> getDictParams() {
		return dictParams;
	}

	public void setDictParams(List<DictParamsDto> dictParams) {
		this.dictParams = dictParams;
	}
	

	public String getBatchCode() {
		return batchCode;
	}

	public void setBatchCode(String batchCode) {
		this.batchCode = batchCode;
	}
	
	

	
	public String getJobCodes() {
		return jobCodes;
	}

	public void setJobCodes(String jobCodes) {
		this.jobCodes = jobCodes;
	}

	public String getPaydayTimeYear() {
		return paydayTimeYear;
	}

	public void setPaydayTimeYear(String paydayTimeYear) {
		this.paydayTimeYear = paydayTimeYear;
	}

	public String getTemplateCode() {
		return templateCode;
	}

	public void setTemplateCode(String templateCode) {
		this.templateCode = templateCode;
	}

	public String getKeyworld() {
		return keyworld;
	}

	public void setKeyworld(String keyworld) {
		this.keyworld = keyworld;
	}

	public String getQueryConutSql() {
		StringBuilder sb = new StringBuilder();
		sb.append("select count(*)  from ").append(tableName).append(" where 1=1 ");
		
		if(this.fileId != null) {
			sb.append(" and file_id = ").append(this.fileId);
		}
		
		if(this.inState != null) {
			sb.append(" and in_state = ").append(this.inState);
		}
		
		if(this.batchCode != null) {
			sb.append(" and batch_code = '").append(this.batchCode).append("'");
		}
		
		if(StringUtils.isNotBlank(this.jobCodes)) {
			String[] jobCodeArr = this.jobCodes.split(",");
			StringBuilder jobCodeParams = new StringBuilder();
			for (String jobCode : jobCodeArr) {
				if(jobCodeParams.length() == 0) {
					jobCodeParams.append(" and job_code in (").append("'").append(jobCode).append("'");
				}else {
					jobCodeParams.append(",").append("'").append(jobCode).append("'");
				}
			}
			if(jobCodeParams.length() != 0) {
				jobCodeParams.append(")");
				sb.append(jobCodeParams);
			}
		}
		
		if(StringUtils.isNotBlank(this.keyworld)) {
			sb.append(" and job_code like ").append("'%").append(this.keyworld).append("%' ");
			if(this.dictParams != null) {
				for (DictParamsDto dictParamsDto : dictParams) {
					if(SalaryConstants.SALARY_JOB_CODE_NAME.equals(dictParamsDto.getDictName())) {
						sb.append("or ").append(dictParamsDto.getDictColumn()).append(" like ").append("'%").append(this.keyworld).append("%' ");
						break;
					}
				}
			}
		}
		
		return sb.toString();
	}
	
	
	public String getQueryDataLimitSql() {
		StringBuilder sb = new StringBuilder();
		if(this.dictParams != null) {
			for (DictParamsDto dictParamsDto : dictParams) {
				if(sb.length() == 0) {
					sb.append("select ").append(dictParamsDto.getDictColumn()).append(" as ").append(dictParamsDto.getDictCode());
				}else {
					sb.append(" , ").append(dictParamsDto.getDictColumn()).append(" as ").append(dictParamsDto.getDictCode());
				}
			}
			sb.append(" from ");
		}else {
			sb.append("select * from ");
		}
		sb.append(tableName).append(" where 1=1 ");
		
		if(this.fileId != null) {
			sb.append(" and file_id = ").append(this.fileId);
		}
		
		if(this.inState != null) {
			sb.append(" and in_state = ").append(this.inState);
		}
		
		if(this.batchCode != null) {
			sb.append(" and batch_code = '").append(this.batchCode).append("'");
		}
		
		if(StringUtils.isNotBlank(this.jobCodes)) {
			String[] jobCodeArr = this.jobCodes.split(",");
			StringBuilder jobCodeParams = new StringBuilder();
			for (String jobCode : jobCodeArr) {
				if(jobCodeParams.length() == 0) {
					jobCodeParams.append(" and job_code in (").append("'").append(jobCode).append("'");
				}else {
					jobCodeParams.append(",").append("'").append(jobCode).append("'");
				}
			}
			if(jobCodeParams.length() != 0) {
				jobCodeParams.append(")");
				sb.append(jobCodeParams);
			}
		}
		
		if(StringUtils.isNotBlank(this.keyworld)) {
			sb.append(" and job_code like ").append("'%").append(this.keyworld).append("%' ");
			if(this.dictParams != null) {
				for (DictParamsDto dictParamsDto : dictParams) {
					if(SalaryConstants.SALARY_JOB_CODE_NAME.equals(dictParamsDto.getDictName())) {
						sb.append("or ").append(dictParamsDto.getDictColumn()).append(" like ").append("'%").append(this.keyworld).append("%' ");
						break;
					}
				}
			}
		}
		
		sb.append(" limit ").append((this.getPage() - 1) * this.getLimit()).append(" , ").append(this.getLimit());
		
		return sb.toString();
	}

	@Override
	public String toString() {
		return "ExcelExportQuerySqlReq [tableName=" + tableName + ", fileId=" + fileId + ", inState=" + inState
				+ ", batchCode=" + batchCode + ", paydayTimeYear=" + paydayTimeYear + ", templateCode=" + templateCode
				+ ", keyworld=" + keyworld + ", dictParams=" + dictParams + "]";
	}
	
}
